/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package me.aartikov.alligator;

import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.render.BlendMode;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.PixelMap.InitializationOptions;

/**
 * 修改图片颜色
 */
public class ImageTint {

    /**
     * 修改element的颜色
     *
     * @param element  需要修改的element
     * @param colorStr 颜色
     * @return 修改完成后的element
     */
    public static Element getTintElement(Element element, String colorStr) {
        int intColor = Color.getIntColor(colorStr);
        return getTintElement(element, intColor);
    }

    /**
     * 修改element的颜色
     *
     * @param element  需要修改颜色的element对象
     * @param intColor 颜色
     * @return Element
     */
    public static Element getTintElement(Element element, int intColor) {
        return getTintElement(element, intColor, BlendMode.SRC_ATOP);
    }

    /**
     * 修改element的颜色
     *
     * @param element  需要修改颜色的element对象
     * @param intColor 颜色
     * @param mode     枚举用于在Canvas上绘制对象的混合模式
     * @return Element
     */
    public static Element getTintElement(Element element, int intColor, BlendMode mode) {
        int[] colors = new int[]{intColor, intColor};
        int[][] states = new int[2][];
        states[0] = new int[]{0};
        states[1] = new int[]{0};
        element.setStateColorList(states, colors);
        element.setStateColorMode(mode);
        return element;
    }

    /**
     * 给image设置一个修改颜色后的element
     *
     * @param view     image对象
     * @param element  需要修改颜色的element
     * @param colorStr 颜色
     */
    public static void setImageTint(Image view, Element element, String colorStr) {
        getTintElement(element, colorStr);
        view.setImageElement(element);
    }

    /**
     * 给image设置一个修改颜色后的element
     *
     * @param view     image对象
     * @param element  需要修改颜色的element
     * @param intColor 颜色
     */
    public static void setImageTint(Image view, Element element, int intColor) {
        getTintElement(element, intColor);
        view.setImageElement(element);
    }


    /**
     * 给image设置一个修改颜色后的element
     *
     * @param view     image对象
     * @param element  需要修改颜色的element
     * @param intColor 颜色
     * @param mode     枚举用于在Canvas上绘制对象的混合模式
     */
    public static void setImageTint(Image view, Element element, int intColor, BlendMode mode) {
        getTintElement(element, intColor, mode);
        view.setImageElement(element);
    }

    /**
     * 修改resourceId对应资源的颜色
     *
     * @param context    上下文
     * @param resourceId 资源id
     * @return PixelMapElement
     */
    public static PixelMapElement getImageElement(Context context, int resourceId) {
        try {
            return new PixelMapElement(getPixmap(context, resourceId));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 修改resourceId对应资源的颜色
     *
     * @param context    上下文
     * @param resourceId 资源id
     * @return 图片资源
     */
    public static PixelMap getPixmap(Context context, int resourceId) {
        try {
            Resource resource = context.getResourceManager().getResource(resourceId);
            return ImageSource.create(resource, null).createPixelmap(null);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 修改resourceId对应资源的颜色
     *
     * @param context      上下文
     * @param resourceId   资源id
     * @param color        需要修改的颜色
     * @param pixmapwidth  图片宽度
     * @param pixmapheight 图片高度
     * @return Element
     */
    public static Element getImagebyColor(Context context, int resourceId, int color, int pixmapwidth, int pixmapheight) {
        try {
            PixelMapElement pixelMap = getImageElement(context, resourceId);
            pixelMap.setBounds(0, 0, pixmapwidth, pixmapheight);
            return getTintElement(pixelMap, color);
        } catch (Exception e) {
            return null;
        }
    }

}
